<?php
namespace Home\Controller;

use Utuo\DistributionShopForeground;
use Agent\Model\AgentModel;

class DisShopCertificateController extends DistributionShopForeground {
	
	/**
	 * 初始化
	 */
	public function _init() {
		layout('disshop');
		$this->setModelName('DistributionShop');
		$this->setModelControllerName('DisShop');
		if ( !in_array(ACTION_NAME, array('doSearch')) ) { // 排除不支持分享跳转的链接
			$this->setSupportRedirect(isset($_REQUEST['debug'])?false:true);
		}
	}
	/**
	 * 查询已申请的代理商
	 */
	public function searchAction() {
		$this->setPageTitle("官方授权查询");
		$this->display("{$this->modelControllerName}/certificateSearch");
	}
	
	/**
	 * 查询已申请的信息
	 */
	public function doSearchAction() {
		$result = array();
		$this->cid or $this->ajaxError('未知商家');
		$tel = I('code', 0) or $this->ajaxError('请输入查询手机');
		
		$agent = M("Agent")->where(array('cid'=>$this->cid, 'tel'=>$tel))->field('name,certificateImg,level')->select();
		if ( $agent ) {
		    foreach ( $agent as &$v  ) {
		        $v['level'] = AgentModel::$agentLevel[$v['level']];
		    }
		}
		$agent and $result['agent'] = $agent; 
		
		$dealer = M("Dealer")->where(array('cid'=>$this->cid, 'tel'=>$tel))->field('name,certificateImg')->select();
		$dealer and $result['dealer'] = $dealer;
		
		if ( $result ) {
			$this->ajaxSuccess('', $result);
		} else {
			$this->ajaxError('您查询的用户授权信息不存在');
		}
	}
}